package com.lmpop.trade.sms;

import com.lmpop.frame.util.Config;
import com.lmpop.frame.util.PropertiesConfigUtil;
import com.lmpop.trade.ti_subscribe.Ti_subscribeInfo;
import com.lmpop.trade.util.VerificationUtil;
import com.lmpop.updateHtml.util.FileUtil;
import java.io.PrintStream;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Message.RecipientType;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import sun.misc.BASE64Encoder;

public class SendMail
{
  public static String username = null;
  public static String password = null;
  public static String smtp_server = null;
  public static String weburl = null;
  public static String rootpath = null;
  public static Config conf = null;

  public boolean EmailToVerification(String email, Map paramMap)
  {
    boolean bl = true;
    try {
      String username = "jettech_liheng@sina.com";
      String password = "liheng19900413";
      String smtp_server = "smtp.sina.com";
      String from_mail_address = username;
      String to_mail_address = "jettech_liheng@sina.com";
      Ti_subscribeInfo subscribe = new Ti_subscribeInfo();

      String configName = "config.properties";
      PropertiesConfigUtil baseConfig = new PropertiesConfigUtil(configName);
      String baseurl = baseConfig.getProperty("weburl");

      Authenticator auth = new PopupAuthenticator(username, password);

      Properties mailProps = new Properties();
      mailProps.put("mail.smtp.auth", "true");
      mailProps.put("username", username);
      mailProps.put("password", password);
      mailProps.put("mail.smtp.host", smtp_server);

      Session mailSession = Session.getDefaultInstance(mailProps, auth);
      MimeMessage message = new MimeMessage(mailSession);
      message.setFrom(new InternetAddress(from_mail_address));
      message.setSubject("邮箱验证");
      MimeMultipart multi = new MimeMultipart();
      BodyPart textBodyPart = new MimeBodyPart();

      SendMail sendmail = new SendMail();
      FileUtil fileutil = new FileUtil();

      paramMap.put("baseurl", baseurl);
      paramMap.put("time", Integer.valueOf(VerificationUtil.getVerificationTime()));
      String htmlfile = fileutil.getContentFromFile(rootpath + "/program/member/index/b2c/verification_email.html");

      Iterator it = paramMap.keySet().iterator();
      while (it.hasNext()) {
        String key = it.next().toString();
        htmlfile = htmlfile.replace("{param." + key + "}", paramMap.get(key).toString());
      }

      htmlfile = sendmail.repalace(htmlfile, weburl);

      message.setContent(htmlfile, "text/html;charset=UTF-8");

      BASE64Encoder enc = new BASE64Encoder();
      message.saveChanges();

      Map map = new Hashtable();
      try
      {
        to_mail_address = email;
        message.setRecipient(Message.RecipientType.TO, new InternetAddress(to_mail_address));

        Transport.send(message);
        System.out.println("向-----------" + to_mail_address + "发送邮件成功-------------------------------------");
      }
      catch (Exception e) {
        bl = false;
        e.printStackTrace();
      }
    } catch (Exception ex) {
      bl = false;
      ex.printStackTrace();
    }
    return bl;
  }

  public static void main(String[] args)
  {
    new SendMail().MainSend();
  }

  public static int SendEmail(String from_mail_address, String to_mail_address, String subject, String content) {
    try {
      Authenticator auth = new PopupAuthenticator(username, password);
      Properties mailProps = new Properties();
      mailProps.put("mail.smtp.auth", "true");
      mailProps.put("username", username);
      mailProps.put("password", password);
      mailProps.put("mail.smtp.host", smtp_server);
      Session mailSession = Session.getDefaultInstance(mailProps, auth);
      MimeMessage message = new MimeMessage(mailSession);
      MimeMultipart mp = new MimeMultipart();

      from_mail_address = username;
      message.setFrom(new InternetAddress(from_mail_address));

      BodyPart body = new MimeBodyPart();
      body.setHeader("Content-Transfer-Encoding", "base64");
      body.setContent(content, "text/html;charset=GBK");
      mp.addBodyPart(body);

      message.setContent(mp);

      BASE64Encoder enc = new BASE64Encoder();
      message.setSubject("=?GBK?B?" + enc.encode(subject.getBytes()) + "?=");

      message.saveChanges();

      message.setRecipient(Message.RecipientType.TO, new InternetAddress(to_mail_address));
      Transport.send(message);
    }
    catch (Exception ex) {
      ex.printStackTrace();
      return -1;
    }
    return 1;
  }

  public void MainSend()
  {
    try {
      String username = "wuyunchanglong@sina.com";
      String password = "zhijiajun253190";
      String smtp_server = "smtp.sina.com";
      String from_mail_address = username;
      String to_mail_address = "wuyunchanglong@sina.com";
      Authenticator auth = new PopupAuthenticator(username, password);
      Properties mailProps = new Properties();
      mailProps.put("mail.smtp.auth", "true");
      mailProps.put("username", username);
      mailProps.put("password", password);
      mailProps.put("mail.smtp.host", smtp_server);
      Session mailSession = Session.getDefaultInstance(mailProps, auth);
      MimeMultipart multi = new MimeMultipart();
      BodyPart textBodyPart = new MimeBodyPart();

      FileUtil fileutil = new FileUtil();
      String htmlfile = fileutil.getContentFromFile(rootpath + "/data/8diantuan/8diantuan.html");

      htmlfile = repalace(htmlfile, weburl);
      htmlfile = repalaceLink(htmlfile, weburl);
      htmlfile = repalaceHref(htmlfile, weburl);

      MimeMessage message = new MimeMessage(mailSession);
      message.setFrom(new InternetAddress(from_mail_address));
      message.setSubject("每天团购信息");
      message.setContent(htmlfile, "text/html;charset=utf-8");
      message.saveChanges();
      Map map = new Hashtable();
      Ti_subscribeInfo subscribe = new Ti_subscribeInfo();
      List list = subscribe.getListByPage(map);

      if ((list != null) && (list.size() > 0))
        for (int i = 0; i != list.size(); i++) {
          map = (Map)list.get(i);
          if (map.get("keyword") == null) continue;
          try {
            to_mail_address = map.get("keyword").toString();
            message.setRecipient(Message.RecipientType.TO, new InternetAddress(to_mail_address));

            Transport.send(message);
          } catch (Exception e) {
            e.printStackTrace();
          }
        }
    }
    catch (Exception ex)
    {
      ex.printStackTrace();
    }
  }

  private String repalace(String file, String weburl) {
    file = file.replaceAll("<img src=\"", "<img src=\"" + weburl);
    return file;
  }

  private String repalaceLink(String file, String weburl) {
    file = file.replaceAll("<link href=\"", "<link href=\"" + weburl);
    return file;
  }

  private String repalaceHref(String file, String weburl) {
    file = file.replaceAll("<a href=\"", "<link a=\"" + weburl);
    return file;
  }

  static
  {
    if (conf == null) {
      conf = new Config();
    }
    username = conf.getString("smtpuser");
    password = conf.getString("smtppwd");
    smtp_server = conf.getString("smtp");
    conf.init("config.properties");
    weburl = conf.getString("weburl");
    rootpath = conf.getString("rootpath");
  }
}